package com.wan37.logic.handler.message;

import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.entity.RoleDO;
import com.wan37.logic.domain.entity.UserDO;
import com.wan37.logic.domain.vo.*;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.listener.EventDispatcher;
import com.wan37.logic.listener.event.EventType;
import com.wan37.logic.listener.event.user.UserLoginEvent;
import com.wan37.logic.service.RoleService;
import com.wan37.logic.service.UserService;
import com.wan37.logic.service.cache.*;
import com.wan37.logic.utils.RoleLevelUtil;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.CommonBase;
import com.wan37.protobuf.message.client2server.game.Game;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户登入数据初始化
 *
 * @author : luoyong
 * @date : 2020-06-22 15:51
 **/
public class CGameUserLoginInitHandler extends MsgHandler {

    private RouteUtils routeUtils;
    private UserServiceCache userServiceCache;
    private SceneServiceCache sceneServiceCache;
    private RoleServiceCache roleServiceCache;
    private UserService userService;
    private RoleLevelUtil roleLevelUtil;
    private RoleService roleService;

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameUserLoginInitHandler(MessageVO messageVO) {
        super(messageVO);
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
        this.sceneServiceCache = SpringContextHolder.getBean("sceneServiceCache");
        this.roleServiceCache = SpringContextHolder.getBean("roleServiceCache");
        this.userService = SpringContextHolder.getBean("userService");
        this.roleLevelUtil = SpringContextHolder.getBean("roleLevelUtil");
        this.roleService = SpringContextHolder.getBean("roleService");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        Game.CGameUserLoginInitMsg initMsg = (Game.CGameUserLoginInitMsg) messageVO.getMessage();
        String userId = initMsg.getSenderId();
        String concurrentSceneId = sceneServiceCache.sceneNameToIdMap.get("启始之地");
        UserServiceCache.User user = userServiceCache.new User();
        UserDO userDO = userService.getUserByUserId(userId);
        UserVO userVO = new UserVO(userDO);
        user.setUserVO(userVO);
        user.setSceneId(concurrentSceneId);
        userServiceCache.getUserServiceMap().put(userId, user);
        userServiceCache.getWorldUserSet().add(userId);
        List<RoleDO> listRoleDO = roleService.getRolesByUserId(userId);
        if (listRoleDO == null || listRoleDO.isEmpty()) {
            routeUtils.sendResponse(Common.GAME_USER_LOGIN_INIT_ERROR, "用户没有角色，请先创建角色！", messageVO.getNetId(), userId);
            return;
        }
        List<String> roleIds = new ArrayList<>();
        RoleVO roleVO = null;
        /**
         * 将角色DO对象转换为VO对象，并进行当前角色缓存初始化
         */
        for (RoleDO roleDO : listRoleDO) {
            roleVO = new RoleVO(roleDO);
            int level = roleDO.getLevel();
            RoleLevelVO roleLevelVO = roleLevelUtil.userRoleLevelMap.get(Integer.toString(level));
            roleVO.getNumberMap().put(CommonBase.ATN, roleLevelVO.getATN());
            roleVO.getNumberMap().put(CommonBase.INT, roleLevelVO.getINT());
            roleVO.getNumberMap().put(CommonBase.DEF, roleLevelVO.getDEF());
            roleVO.getNumberMap().put(CommonBase.RES, roleLevelVO.getRES());
            roleVO.setStatus(roleDO.getStatus());
            roleIds.add(roleVO.getAllRoleId());
            RoleServiceCache.Role role = roleServiceCache.new Role();
            role.setRoleVO(roleVO);
            roleServiceCache.getRoleServiceMap().put(roleVO.getAllRoleId(), role);
        }
        userServiceCache.getUserServiceMap().get(userId).setRoleIdList(roleIds);
        /**
         * 触发用户登入事件
         */
        EventDispatcher.getInstance().fireEvent(new UserLoginEvent(EventType.LOGIN, userId));
        routeUtils.sendResponse(Common.GAME_USER_LOGIN_INIT_SUCCESS, "初始化成功！", messageVO.getNetId(), userId);
    }
}
